<?php 
/**
 *  每天定时将 关注拍品数量 投标拍品数量 竞拍拍品数量 成交数量 登录次数 数据插入au_dealer_statistics车商统计表
 */
date_default_timezone_set("Asia/Shanghai");

set_time_limit(0);

if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );

$start = $argv[1];

$end = $argv[2];

$days = abs(strtotime($start) - strtotime($end))/86400;

for ($i=0; $i <= $days; $i++) { 

	$Yesterday = date('Y-m-d',strtotime("+".$i." day",strtotime($start)));

    $start_time = $Yesterday." 00:00:00";

    $end_time = $Yesterday." 23:59:59";

    paoshuju($Yesterday,$start_time,$end_time);
}

function paoshuju($Yesterday,$start_time,$end_time){

	global $db;

	$dealer = array();

	//登录次数

	$login_data = get_login_info($start_time,$end_time);

	foreach ($login_data as $k => $v) {

		if (!isset($dealer[$v['dealer_id']]['login_num'])) {

			$dealer[$v['dealer_id']]['login_num'] = 1 ;
		}
		else{
			$dealer[$v['dealer_id']]['login_num']++;
		}
		
	}

	//关注拍品数量  
	$follow_data = get_follow_info($start_time,$end_time);

	foreach ($follow_data as $k1 => $v1) {

		if (!isset($dealer[$v1['dealer_id']]['follow_num'])) {

			$dealer[$v1['dealer_id']]['follow_num'] = 1 ;
		}
		else{
			$dealer[$v1['dealer_id']]['follow_num']++;
		}
	}

	//投标拍品数量

	$bid_data = get_bid_info($start_time,$end_time);


	$new_dealer = array();

	foreach ($bid_data as $k2 => $v2) {

		if ($v2['createtime'] >= $start_time) {

			if (!isset($dealer[$v2['dealer_id']]['bid_num'])) {

				$dealer[$v2['dealer_id']]['bid_num'] = 1 ;
			}
			else{
				$dealer[$v2['dealer_id']]['bid_num']++;
			}
		}elseif($v2['createtime'] < $start_time){

			$newdate = date("Y-m-d",strtotime($v2['createtime']));

			if (!isset($new_dealer[$v2['dealer_id']][$newdate])) {

				$new_dealer[$v2['dealer_id']][$newdate] = 1 ;
			}
			else{
				$new_dealer[$v2['dealer_id']][$newdate]++;
			}
		}
	}


	//竞拍拍品数量
	$bidding_data = get_bidding_info($start_time,$end_time);

	foreach ($bidding_data as $k3 => $v3) {

		if (!isset($dealer[$v3['dealer_id']]['bidding_num'])) {

			$dealer[$v3['dealer_id']]['bidding_num'] = 1 ;
		}
		else{
			$dealer[$v3['dealer_id']]['bidding_num']++;
		}
	}

	//成交数量
	$succ_data = get_deal_info($start_time,$end_time);

	foreach ($succ_data as $k4 => $v4) {

		if (!isset($dealer[$v4['dealer_id']]['succ_num'])) {

			$dealer[$v4['dealer_id']]['succ_num'] = 1 ;
		}
		else{
			$dealer[$v4['dealer_id']]['succ_num']++;
		}
	}

	if ($dealer) {
		
		foreach ($dealer as $k5 => $v5) {
			
			//找到车商对应emp_id
			$emp_id_sql = "select emp_id from au_dealer_manager where dealer_id = '".$k5."'";

			$emp_id = $db ->fetchOne($emp_id_sql);
			//找到车商对应的uid
			$uid_sql = "select du.uid,u.status from au_dealer_user as du ,au_user as u where dealer_id = '".$k5."' and du.uid = u.uid ";

			$uid = $db ->fetchOne($uid_sql);

			if ($uid && $uid['status'] == 0) {
				$is_exists_sql = "select id from au_dealer_statistics where dealer_id = '".$k5."' and date ='".$Yesterday."' ";
				
				$is_exists_data = $db ->fetchOne($is_exists_sql);

				if (!$is_exists_data) {					
					@$query_sql = "insert into au_dealer_statistics (`dealer_id`,`follow_num`,`bid_num`,`Auction_num`,`deal_num`,`login_num`,`emp_id`,`uid`,`date`,`createtime`) values ('".$k5."','".$v5['follow_num']."','".$v5['bid_num']."','".$v5['bidding_num']."','".$v5['succ_num']."','".$v5['login_num']."','".$emp_id['emp_id']."' ,'".$uid['uid']."','".$Yesterday."',now()) ";

					@print_r("insert---".$Yesterday.":车商id".$k5.",关注数量:".$v5['follow_num'].",投标数量:".$v5['bid_num'].",竞标数量:".$v5['bidding_num'].",成交数量:".$v5['succ_num'].",登陆数量:".$v5['login_num']."\n");
					// echo $query_sql;
					$ok = $db ->query($query_sql);
				}
			}
		}
	}

	if ($new_dealer) {
		
		foreach ($new_dealer as $k6 => $v6) {

			foreach ($v6 as $k7 => $v7) {

				$select_sql = "select * from au_dealer_statistics where dealer_id ='".$k6."' and `date` = '".$k7."'";

				$is_exists_bsql = $db ->fetchOne($select_sql);

				if (!$is_exists_bsql) {
					
					@$b_sql = "insert into au_dealer_statistics (`dealer_id`,`bid_num`,`date`,`createtime`) values ('".$k6."','".$v7."','".$k7."',now())";

				}else{

					@$b_sql = "update au_dealer_statistics set bid_num = bid_num +'".$v7."' where dealer_id ='".$k6."' and `date` = '".$k7."'";
					
				}

				@print_r("update---".$k7.":车商id".$k6.",投标数量增加:".$v7."\n");

				$ok1 = $db ->query($b_sql);
			}
		}
	}

	$db->close();

	unset($db);  

	$db = null;
}
// while (true) {

	

//     //睡觉--------------------------------
//     $sleeptimes = getNextSleepSecond();

//     print_r("sleep:".$sleeptimes."\n");

//     sleep($sleeptimes);
    
//     continue;
// }

// $date1 = date('Y-m-d H:i:s');

// print_r("----------------------endtime:$date1 ------------------------------------------------- \n");


//登陆信息
function get_login_info($start_time,$end_time){

	global $db;

	$login_sql = "select du.dealer_id from au_login_log as ll ,au_dealer_user as du where du.uid = ll.uid and du.status = 1 and  ll.login_time >= '".$start_time."' and ll.login_time <= '".$end_time."' ";

	$login_data = $db ->fetchAll($login_sql);

	return $login_data;
}

//关注拍品信息
function get_follow_info($start_time,$end_time){

	global $db;

	$follow_sql = "select df.dealer_id,df.order_id from au_dealer_follow as df ,au_order as o,au_cars as c where o.bid_start_time <= '".$end_time."'  and o.bid_start_time >= '".$start_time."' and df.order_id = o.order_id and o.car_id = c.car_id  group by df.order_id,df.dealer_id";

	$follow_data = $db ->fetchAll($follow_sql);

	return $follow_data;
}
	
//投标拍品信息
function get_bid_info($start_time,$end_time){

	global $db;

	$bid_sql = "select bl.dealer_id,bl.order_id,GROUP_CONCAT(bl.createtime ORDER BY bl.createtime DESC) as createtime from au_bid_log as bl,au_order as o,au_cars as c where bl.order_id = o.order_id and o.car_id = c.car_id  and o.bidding_end_time >= '".$start_time."' and o.bidding_end_time <='".$end_time."' group by bl.order_id,bl.dealer_id";

	$bid_data = $db ->fetchAll($bid_sql);

	foreach ($bid_data as $key => $value) {

		@$bid_data[$key]['createtime'] = explode(',', $value['createtime'])[0];
	}

	return $bid_data;
}
//竞标拍品信息
function get_bidding_info($start_time,$end_time){

	global $db;

	$bidding_sql = "select bl.dealer_id,bl.order_id from au_bidding_log as bl,au_order as o,au_cars as c where bl.createtime >= '".$start_time."' and bl.createtime <= '".$end_time."' and bl.order_id = o.order_id and o.car_id = c.car_id group by bl.order_id,bl.dealer_id";

	$bidding_data = $db ->fetchAll($bidding_sql);

	return $bidding_data;
}

//成交拍品信息
function get_deal_info($start_time,$end_time){

	global $db;

	$deal_sql = "select da.dealer_id,da.order_id from au_dealer_auctioned as da,au_order as o,au_cars as c where da.createtime >= '".$start_time."' and da.createtime <= '".$end_time."' and da.order_id = o.order_id and o.car_id = c.car_id and da.status = 0 and o.status = 13 group by da.order_id,da.dealer_id";
	$deal_data = $db ->fetchAll($deal_sql);

	return $deal_data;
}
